package com.zy.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zy.common.Result;
import com.zy.entity.Category;
import com.zy.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * <p>
 * 菜品及套餐分类 前端控制器
 * </p>
 *
 * @author xyz
 */
@RestController
@RequestMapping("/category")
@Slf4j
@Api(tags = "分类相关接口")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增菜品分类
     * @param category 接收菜品分类对象
     * @return
     */
    @ApiOperation(value = "新增菜品分类接口")
    @PostMapping("")
    public Result<Category> save(@RequestBody Category category){
        log.info("新增菜品分类");
        categoryService.save(category);
        return Result.success(category);
    }

    /**
     * 菜品列表功能
     * @param page 第几页
     * @param pageSize 每页查几条数据
     * @return
     */
    @ApiOperation(value = "分页查询菜品分类接口")
    @GetMapping("page")
    public Result<Page> listCategory(int page, int pageSize){
        //分页构造器
        Page pageInfo = new Page(page, pageSize);
        //过滤条件
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByDesc(Category::getSort);
        categoryService.page(pageInfo,lambdaQueryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 删除菜品种类
     * @param id 分类ID
     * @return
     */
    @ApiOperation(value = "删除菜品种类接口")
    @DeleteMapping()
    public Result<String> delCategory(Long id){
        categoryService.removeCategory(id);
        return Result.success("删除成功");
    }

    /**
     * 更新菜品分类
     *
     * @param category 传回来的菜品分类对象
     */
    @ApiOperation(value = "更新菜品分类接口")
    @PutMapping()
    public Result<String> updateCategory(@RequestBody Category category) {
        log.info("更新种类{}", category);
        categoryService.updateById(category);
        return Result.success("菜品种类更新完成");
    }

    /**
     * 菜品新增页面菜品下拉列表
     * @param category 从前端接收一个type=1的标注，目的是在分类表中，菜品分类是1，套餐分类是2，把二者区分开
     * @return
     */
    @ApiOperation(value = "查询指定分类的套餐或菜品接口")
    @GetMapping("/list")
    public Result<List> listShowCategory(Category category){
        //条件构造器
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper();
        //MP也支持把对象先判断一下，非空才能进行查询
        lambdaQueryWrapper.eq(category.getType() != null, Category::getType, category.getType());
        //按时间倒叙排序
        lambdaQueryWrapper.orderByAsc(Category::getSort).orderByDesc(Category::getUpdateTime);
        //查询
        List<Category> categoryList = categoryService.list(lambdaQueryWrapper);
        return Result.success(categoryList);
    }
}
